
    \foldertitle{trec}{Time-Recursive Expressions (trec Objects)}{trec/Contents}

	Time-recursive subscript objects (trec objects) allow creating and
evaluating time-recursive expressions based on
\href{tseries/Contents}{tseries} objects. Read below carefully when IRIS
fails to evaluate time-recursive expessions correctly.

Trec methods:

\paragraph{Constructor}

\begin{itemize}
\tightlist
\item
  \href{trec/trec}{\texttt{trec}} - Create new recursive time subscript
  object.
\end{itemize}

\paragraph{Creating lags and leads}

\begin{itemize}
\tightlist
\item
  \href{trec/plus}{\texttt{plus}} - Create time-recursive lead of
  tseries object.
\item
  \href{trec/minus}{\texttt{minus}} - Create time-recursive lag of
  tseries object.
\end{itemize}

\paragraph{Using Time-Recursive
Subscripts}

Time-recursive expressions are expressions that are evaluated period by
period, with each result assigned immediately to the left-hand side
tseries variable, and used in subsequent periods evaluated afterwards.

To construct and evaluate time-recursive expressions, use tseries
referenced by a trec object, or a lag or lead created from a trec
object. Every tseries object on both the left-hand side (i.e.~the
variable under construction) and the right-hand side (i.e.~the variables
in the expression that is being evaluated) must be referenced by a trec
object (or possibly a lag or lead). When referencing a tseries object by
a trec, you can use either curly braces, \texttt{\{...\}}, or round
brackets, \texttt{(...)}; there is no difference between them in
time-recursive expressions.

\(\attention\) See the description below of situations when IRIS fails
to evaluate time-recursive expressions correctly, and how to avoid/fix
such situations.

\paragraph{Example}

Construct an autoregressive sequence starting from an initial value of
10 with a autoregressive coefficient 0.8 between periods 2010Q1 and
2020Q4:

\begin{verbatim}
T = trec(qq(2010,1):qq(2020,4));
x = tseries(qq(2009,4),10);
x{T} = 0.8*x{T-1};
\end{verbatim}

\paragraph{Example}

Construct a first-order autoregressive process, \texttt{x}, with
normally distributed innovations, \texttt{e}:

\begin{verbatim}
T = trec(qq(2010,1):qq(2020,4));
x = tseries(qq(2009,4),10);
e = tseries(qq(2010,1):qq(2020,4),@randn);
x{T} = (1-0.8)*10 + 0.8*x{T-1} + e{T};
\end{verbatim}

\paragraph{Example}

Construct a second-order log-autoregressive process going backward from
year 2020 to year 2000.

\begin{verbatim}
T = trec(yy(2020):-1:yy(2000));
b = tseries();
b(yy(2022)) = 1.56;
b(yy(2021)) = 1.32;
b{T} = b{T+1}^1.2 / b{T+2}^0.6;
\end{verbatim}

\paragraph{Example}

Construct the first 20 numbers of the Fibonacci sequence:

\begin{verbatim}
 T = trec(3:20);
 f = tseries(1:2,1);
 f{T} = f{T-1} + f{T-2};
\end{verbatim}

\paragraph{When IRIS Fails to Evaluate Time-Recursive Expressions
Correctly}

\(\attention\) IRIS fails to evaluate time-recursive expressions
correctly (without any indication of an error) when the following two
circumstances occur \emph{at the same time}:

\begin{itemize}
\item
  At least one tseries object on the right-hand side has been created by
  copying the left-hand side tseries object with no further
  manipulation.
\item
  The time series used in the expression are within a database (struct),
  or a cell array;
\end{itemize}

Under these circumstances, the right-hand side tseries variable will be
assigned (updated with) the results calculated in iteration as if it
were the tseries variable on the left-hand side.

\paragraph{Example}

Create a database with two tseries. Create one of the tseries by simply
copying the other (i.e.~plain assignment with no further manipulation).

\begin{verbatim}
d = struct();
d.x = tseries(1:10,1);
d.y = d.x;

T = trec(2:10);
d.x{T} = 0.8*d.y{T-1}; % Fails to evaluate correctly.
\end{verbatim}

The above time-recursive expression will be incorrectly evaluated as if
it were \texttt{d.x\{T\}\ =\ 0.8*d.x\{T-1\}}. However, when the tseries
objects are not stored within a database (struct) but exist as
stand-alone variables, the expression will evaluate correctly:

\begin{verbatim}
x = tseries(1:10,1);
y = x;

T = trec(2:10);
x{T} = 0.8*y{T-1}; % Evaluates correctly.
\end{verbatim}

\paragraph{Workaround when Time-Recursive Expressions
Fail}

\(\attention\) To evaluate the expression correctly, simply apply any
kind of operator or function to the tseries \texttt{d.y} before it
enters the time-recursive expression. Below are examples of some simple
manipulations that do the job without changing the tseries \texttt{d.y}:

\begin{verbatim}
d = struct();
d.x = tseries(1:10,1);
d.y = 1*d.x;
\end{verbatim}

or

\begin{verbatim}
d = struct();
d.x = tseries(1:10,1);
d.y = d.x{:};
\end{verbatim}

or

\begin{verbatim}
d = struct();
d.x = tseries(1:10,1);
d.y = d.x;
d.y = d.y + 0;
\end{verbatim}

Reference page for trec



